package com.example.finaltool.utils;

import com.example.finaltool.consts.NamePart;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dengbw
 * @date 2021/8/19 9:46 上午
 */
public class CorpUtils {
    private CorpUtils(){}

    public static Map<String, String> toParts(String name) {
        Map<String, String> map = new HashMap<>(4);

        String[] s1 = name.split("省");
        String sheng = s1[0];
        map.put(NamePart.SHENG, sheng + "省");
        String rest = s1[1];

        String[] s2 = rest.split("市");
        String shi = s2[0];
        if (s2.length < 2) {
            return map;
        }
        rest = s2[1];
        if (s2.length > 2) {
            rest = s2[1] + "市" + s2[2];
        }
        getQuxianZhen(map, rest);
        map.put(NamePart.SHI, shi + "市");
        return map;
    }

    private static void getQuxianZhen(Map<String, String> map, String rest) {
        if (rest.contains("瓦房店")) {
            map.put(NamePart.QU_XIAN, "瓦房店");
            rest = rest.replace("瓦房店", "").replace("市", "").replace("区", "").replace("县", "");
            if (rest.contains("社区") || rest.contains("村")) {
                saveZhen(map, rest);
            } else {
                map.put(NamePart.XIANG_ZHEN, rest);
            }
            return;
        }
        rest = rest.replace("社区", "");
        List<String> target = new ArrayList<>(List.of("区", "县", "市"));
        String finalRest = rest;
        for (String t : target) {
            if (finalRest.contains(t)) {
                String[] s = finalRest.split(t);
                map.put(NamePart.QU_XIAN, s[0] + t);
                rest = s[1];
                saveZhen(map, rest);
                break;
            }
        }
    }

    private static void saveZhen(Map<String, String> map, String rest) {
        List<String> target = new ArrayList<>(List.of("镇", "乡", "街道"));
        target.forEach(t -> {
            if (rest.contains(t)) {
                String[] s = rest.split(t);
                map.put(NamePart.XIANG_ZHEN, s[0] + t);
            }
        });
    }
}
